package com.fast.develop.meta.resource.loader;

import com.fast.develop.meta.resource.Func;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author 莫寅
 * @createTime 2022-06-04 13:31
 * @desc：
 */
public class ResourceFuncParser {


    private static ResourceFuncParser resourceFuncParser;

    public static ResourceFuncParser getInstance(){
        if(resourceFuncParser == null){
            resourceFuncParser = new ResourceFuncParser();
        }
        return resourceFuncParser;
    }

    public Func parse(JSONObject rawSchema){

        Func func = new Func();
        bindId(rawSchema, func);
        bindTitle(rawSchema, func);
        bindChild(rawSchema, func);

        return func;
    }


    private void bindId(JSONObject rawSchema, Func func){
        func.setId(rawSchema.optString("id"));
    }

    private void bindTitle(JSONObject rawSchema, Func func){
        func.setTitle(rawSchema.optString("title"));
    }


    private void bindType(JSONObject rawSchema, Func func){
        func.setTitle(rawSchema.optString("type"));
    }


    private void bindChild(JSONObject rawSchema, Func func){

        JSONArray jsonArray = rawSchema.optJSONArray("child");
        if(Objects.nonNull(jsonArray)){
            jsonArray.forEach(o -> {
                JSONObject jsonObject = (JSONObject)o;

                Func runc = ResourceFuncParser.getInstance().parse(jsonObject);
                func.getChild().add(runc);
            });
        }
    }



}
